package com.sun.mail.iap;

import com.getpebble.android.R;
import com.sun.mail.util.ASCIIUtility;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ResponseInputStream {
    private static final int incrementSlop = 16;
    private static final int maxIncrement = 262144;
    private static final int minIncrement = 256;
    private BufferedInputStream bin;

    public ResponseInputStream(InputStream in) {
        this.bin = new BufferedInputStream(in, 2048);
    }

    public ByteArray readResponse() throws IOException {
        return readResponse(null);
    }

    public ByteArray readResponse(ByteArray ba) throws IOException {
        if (ba == null) {
            ba = new ByteArray(new byte[128], 0, 128);
        }
        byte[] buffer = ba.getBytes();
        int idx = 0;
        while (true) {
            int i;
            int avail;
            int b = 0;
            boolean gotCRLF = false;
            int idx2 = idx;
            while (!gotCRLF) {
                int count;
                b = this.bin.read();
                if (b != -1) {
                    switch (b) {
                        case R.styleable.WalletFragmentStyle_maskedWalletDetailsLogoImageType /*10*/:
                            if (idx2 > 0 && buffer[idx2 - 1] == (byte) 13) {
                                gotCRLF = true;
                                break;
                            }
                    }
                    if (idx2 >= buffer.length) {
                        int incr = buffer.length;
                        if (incr > maxIncrement) {
                            incr = maxIncrement;
                        }
                        ba.grow(incr);
                        buffer = ba.getBytes();
                    }
                    idx = idx2 + 1;
                    buffer[idx2] = (byte) b;
                    idx2 = idx;
                } else if (b != -1) {
                    throw new IOException();
                } else {
                    if (idx2 >= 5 && buffer[idx2 - 3] == (byte) 125) {
                        i = idx2 - 4;
                        while (i >= 0 && buffer[i] != (byte) 123) {
                            i--;
                        }
                        if (i < 0) {
                            try {
                                count = ASCIIUtility.parseInt(buffer, i + 1, idx2 - 3);
                                if (count <= 0) {
                                    avail = buffer.length - idx2;
                                    if (count + incrementSlop <= avail) {
                                        ba.grow(minIncrement <= (count + incrementSlop) - avail ? minIncrement : (count + incrementSlop) - avail);
                                        buffer = ba.getBytes();
                                        idx = idx2;
                                    } else {
                                        idx = idx2;
                                    }
                                    while (count > 0) {
                                        int actual = this.bin.read(buffer, idx, count);
                                        count -= actual;
                                        idx += actual;
                                    }
                                } else {
                                    idx = idx2;
                                }
                            } catch (NumberFormatException e) {
                            }
                        }
                    }
                    ba.setCount(idx2);
                    return ba;
                }
            }
            if (b != -1) {
                i = idx2 - 4;
                while (i >= 0) {
                    i--;
                }
                if (i < 0) {
                    ba.setCount(idx2);
                    return ba;
                }
                count = ASCIIUtility.parseInt(buffer, i + 1, idx2 - 3);
                if (count <= 0) {
                    idx = idx2;
                } else {
                    avail = buffer.length - idx2;
                    if (count + incrementSlop <= avail) {
                        idx = idx2;
                    } else {
                        if (minIncrement <= (count + incrementSlop) - avail) {
                        }
                        ba.grow(minIncrement <= (count + incrementSlop) - avail ? minIncrement : (count + incrementSlop) - avail);
                        buffer = ba.getBytes();
                        idx = idx2;
                    }
                    while (count > 0) {
                        int actual2 = this.bin.read(buffer, idx, count);
                        count -= actual2;
                        idx += actual2;
                    }
                }
            } else {
                throw new IOException();
            }
        }
    }
}
